/*
 * Copyright kecp
 * Jun 01, 2010
 */

package traveller;
import LBS.*;

/**
 * 旅游者
 * @author kecp
 */
public class Traveller {
    /**
     * 起始点
     */
    Location start;
    /**
     * 当前位置
     */
    private Location cur;
    /**
     * 目标位置
     */
    Location dest;

    /**
     * 精确范围
     */
    int range;

    /**
     * 位置服务
     */
    LocationService loctSrv;

    TravelListener travListener;

    public Traveller( LocationService ls ) {
        loctSrv = ls;
        start = new Location();
        cur = new Location();
        dest =  new Location();
        range = 0;
    }

    public void setTravelListener(TravelListener travL) {
        travListener = travL;
    }

    public void setService( LocationService ls ) {
        loctSrv = ls;
    }

    /**
     * 更新当前位置
     * @return 当位置有改变时返回true同时通知监听对象位置改变事件,否则返回false
     */
    public  boolean updateCurLoct(){
        boolean changed = false;
        Location loct;
        Location preLoct = loctSrv.getPrevLocation();
        loct = loctSrv.getCurLocation();
        if( loct!= null )
            cur = loct;
        changed = (preLoct != loct) ;
        if(changed) //位置改变
            if(travListener != null)
                travListener.curChanged(cur); //通知事件
        return changed;
    }

    /**
     * 检测是否在范围内
     * @return 是否在范围内
     */
    public boolean isInRange() {
        //当前位置与目的点位置距离计算
        int dis = Location.calDistance( getCur(),dest);
        if( dis <=  range )
            return true;
        else
            return false;
    }

    public int getDistance() {
        
        return Location.calDistance(getCur(), dest);
    }

    /**
     * 通知到达事件
     */
    public void fireArrived() {

    }

    public boolean checkArrive() {
        updateCurLoct();
        if( isInRange() ) {
            fireArrived();
            return true;
        }
        else
            return false;
    }

    /**
     * @return the cur
     */
    public Location getCur() {
        return cur;
    }

    public void setCur(Location loct) {
        if(loct == cur)
            return;
        cur = loct;
        travListener.curChanged(cur);
    }
}


